package com.ang.hutool.excel.simpleDemos;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.poi.excel.ExcelUtil;
import cn.hutool.poi.excel.ExcelWriter;

import java.util.*;
import java.util.stream.IntStream;

/**
 * @author ssang
 * @date 2021-09-04 14:25
 */
public class WriterBean {
    public static void main(String[] args) {
        WriterBean beanWriter = new WriterBean();
        beanWriter.writerExcel2();
    }

    public void writerExcel(){
        List<TestBean> rows = getRows();
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/writeBean.xlsx");

        //自定义标题别名，不定义别名，标题将是：name，age，score...
        writer.addHeaderAlias("name", "姓名");
        writer.addHeaderAlias("age", "年龄");
        writer.addHeaderAlias("score", "分数");
        //writer.addHeaderAlias("pass", "是否通过");
        writer.addHeaderAlias("examDate", "考试时间");

        //表示只导出定义了别名的字段，比如上面的pass字段没有定义别名的话，是不会输出到文档，如果注释掉下面，pass即使没有定义别名，也会输出到文档
        writer.setOnlyAlias(true);

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(4, "一班成绩单");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        // 关闭writer，释放内存
        writer.close();
    }

    public void writerExcel2(){
        List<TestBean> rows = getRows();
        // 通过工具类创建writer
        ExcelWriter writer = ExcelUtil.getWriter("d:/writeBean.xlsx");

        //自定义标题别名，不定义别名，标题将是：name，age，score...
        //为了保证顺序，使用LinkedHashMap
        Map<String,String> aliasMap  = new LinkedHashMap<>();
        aliasMap.put("name", "姓名");
        aliasMap.put("age", "年龄");
        aliasMap.put("score", "分数");
        aliasMap.put("examDate", "考试时间");

        writer.setHeaderAlias(aliasMap);

        //表示只导出定义了别名的字段，比如上面的pass字段没有定义别名的话，是不会输出到文档，如果注释掉下面，pass即使没有定义别名，也会输出到文档
        writer.setOnlyAlias(true);

        // 合并单元格后的标题行，使用默认标题样式
        writer.merge(aliasMap.size()-1, "一班成绩单");
        // 一次性写出内容，使用默认样式，强制输出标题
        writer.write(rows, true);
        // 关闭writer，释放内存
        writer.close();
    }

    /**
     * 构建数据
     * List<List<String>>
     *     每个单元格是一个String
     * @return
     */
    public List<TestBean> getRows(){
        TestBean bean1 = new TestBean();
        bean1.setName("张三");
        bean1.setAge(22);
        bean1.setPass(true);
        bean1.setScore(66.30);
        bean1.setExamDate(DateUtil.date());

        TestBean bean2 = new TestBean();
        bean2.setName("李四");
        bean2.setAge(28);
        bean2.setPass(false);
        bean2.setScore(38.50);
        bean2.setExamDate(DateUtil.date());

        List<TestBean> rows = CollUtil.newArrayList(bean1, bean2);

        return rows;
    }

    /**
     * 获取list
     */
    public List<TestBean> getList(){
        List<TestBean> rows = new ArrayList<>();

        IntStream.range(0,10).forEach(i->{
            TestBean bean = new TestBean();
            bean.setName("张三");
            bean.setAge(22);
            bean.setPass(true);
            bean.setScore(66.30);
            bean.setExamDate(DateUtil.date());

            rows.add(bean);
        });

        return rows;
    }

    /**
     * 测试bean
     */
    class TestBean{
        private String name;
        private int age;
        private boolean pass;
        private double score;
        private Date examDate;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getAge() {
            return age;
        }

        public void setAge(int age) {
            this.age = age;
        }

        public boolean isPass() {
            return pass;
        }

        public void setPass(boolean pass) {
            this.pass = pass;
        }

        public double getScore() {
            return score;
        }

        public void setScore(double score) {
            this.score = score;
        }

        public Date getExamDate() {
            return examDate;
        }

        public void setExamDate(Date examDate) {
            this.examDate = examDate;
        }
    }
}
